<?php
namespace app\api\controller;
use app\common\model\Message as MessageModel;
use app\common\model\UserMessage as UserMessageModel;
class Message extends Base
{
    protected $noNeedLogin = ["free_count"];
    //获取本人的type=1已读 2未读
    public function get_list()
    {
        $uid = $this->auth->id;
        //获取关于自己的消息
        $message_model = new MessageModel();
        $user_message_model = new UserMessageModel();
        //查询用户已读表
        $read = $user_message_model->where('uid',$uid)->column('message_id');
        $type  = $this->request->get('type',1);
        $where=[];
        $where['status'] = 1;
        //查看未读消息
//        if($type == 1){
//            if($read){
//                $where['id'] =['not in',$read];
//            }
//            $list = $message_model->where($where)->whereOr(function ($query)use($uid,$read,$where) {
//                $query->whereOr("FIND_IN_SET($uid,user_ids) and type=2");
//                $query->whereOr("type=1");
//            })->order('create_time desc')->paginate(10);
////            $list = $message_model->where($where)->order('create_time desc')->paginate(10);
//        }else{
//            //已读消息
//            $list = $user_message_model
//                ->alias('u')
//                ->join('message m','u.message_id=m.id','left')
//                ->order('u.createtime desc')
//                ->paginate(10);
//        }
        $list = $message_model->where($where)->whereOr(function ($query)use($uid,$read,$where) {
        $query->whereOr("FIND_IN_SET($uid,user_ids) and type=2");
        $query->whereOr("type=1");
        })->order('create_time desc')->paginate(10);
        $list = json_decode(json_encode($list),true);
        if($list['data']){
            foreach ($list['data'] as &$v){
                $v['is_read']=0;
                if($read){
                    if(in_array($v['id'],$read)){
                        $v['is_read']=1;
                    }
                }

                if($v['image']){
                    $v['image'] = cdnurl($v['image'],true);
                }
                $v['create_time'] = date('Y-m-d H:i',$v['create_time']);
            }
        }
        $this->success('获取成功',$list,200);
    }

    //详情
    public function detail()
    {
        $id = $this->request->post('id');//消息id
        if(!$id){
            $this->error('参数错误');
        }
        $message_model = new MessageModel();
        $info = $message_model->where('id',$id)->find();
        if(!$info){
            $this->error('系统消息已被隐藏');
        }
        $uid = $this->auth->id;
        $user_message_model = new UserMessageModel();
        if($uid){
            $my_info = $user_message_model->where(['uid'=>$uid,'message_id'=>$id])->find();
            if(!$my_info){
                $data=[
                    'message_id'=>$id,
                    'createtime'=>time(),
                    'uid'=>$uid
                ];
                $user_message_model->save($data);
            }else{
                $user_message_model->where(['uid'=>$uid,'message_id'=>$id])->update(['updatetime'=>time()]);
            }

        }
        if($info->image){
            $info->image=cdnurl($info->image,true);
        }
        $info->create_time=date('Y-m-d H:i',$info->create_time);
        $this->success('获取成功',$info,200);
    }

    //查看剩余未读消息多少条
    public function free_count()
    {
        $uid = $this->auth->id;
        $list = [];
        if(!$uid){
            $this->success('获取成功',$list,200);
        }
        $message_model = new MessageModel();
        $user_message_model = new UserMessageModel();
        $where=[];
        $where['status'] = 1;
        $read = $user_message_model->where('uid',$uid)->column('message_id');
//        if($read){
//            $where['id'] =['not in',$read];
//        }
        $total = $message_model->whereOr(function ($query)use($uid,$read,$where) {
            $query->whereOr("FIND_IN_SET($uid,user_ids) and type=2");
            $query->whereOr("type=1");
        })->where($where)->count();
        $read = $user_message_model->where('uid',$uid)->count();
        $free_count = $total - $read;

        $list['message'] = $free_count;
        //查询所有未读的评论
        $comment_model = new \app\common\model\Comment();
        $art_ids = db('article')->where('user_id',$uid)->column('id');
        $whereOr=[];
        if($art_ids){
            $whereOr['c.aid']=['in',$art_ids];
            $whereOr['c.user_id']=['neq',$uid];
        }
        $comment_count = $comment_model
            ->alias('c')
            ->where('is_read',0)
            ->where(function ($query)use($whereOr,$uid){
                $query->where($whereOr)->whereOr('to_user_id',$uid);
//                $query->where('to_user_id',$uid);
            })
            ->count();
        $list['comment'] = $comment_count;

        $this->success('获取成功',$list,200);
    }


    //查看需不需要显示 消息按钮
//    public function is_show()
//    {
//        $uid = $this->auth->id;
//        if($uid){
//            $this->success('用户信息不存在',0,200);
//        }
//
//    }
}